package cn.itcast.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import cn.itcast.factory.YamlPropertytySourceFactory;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

//四个配置类代表四个配置文件
@Configuration
@PropertySource(value = "classpath:jdbc.yml",factory = YamlPropertytySourceFactory.class)
//@PropertySource(value = "classpath:jdbc.properties")
public class MybatisConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    //设置数据源的bean
    @Bean("dataSource")
    public DataSource creatDataSource(){
        ComboPooledDataSource com = new ComboPooledDataSource();
        try {
            com.setDriverClass(driver);
            com.setJdbcUrl(url);
            com.setUser(username);
            com.setPassword(password);
            return com;
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return null;
    }

}
